/**
 * @author Samsung
 */


function GCcheckData(data)
{
	alert('data');
	printObject(data);
    if (data.status)
    {
        if (data.status == 'ok'){
            alert('Server Response Status : OK');
            return true;
        }
        else
        {
            alert('Server Response Status : SERWER ERROR');
            printObject(data.message);
        }
    }
    else
        alert('Server Response Status : Unknown error');
    return false;
}
function GCProfilesListed(data)
{
    if (GCcheckData(data))
    {
        delete data.status;
        var profileArr = new Array();
        for (var profile in data.preferences)
            profileArr.push(data.preferences[profile].toString());
        GData.profileList = profileArr;
        GlobalContainer[0].objects[0][0].refreshData(GData.profileList);
		GlobalContainer[0].objects[0][0].setIndex(0);
    }
}

function GCSuggestions(data)
{
	// http://simple-interactive-tv.appspot.com/suggestions?brutality=4&romantism=3&comedy=1&startTime=12380&endTime=12498
	// data = '{"3":"tytul2","2":"tytul1","1":"tytul3","0":"tytul6","status":"ok","5":"tytul4","4":"tytul5"}';
    if (GCcheckData(data))
    {
		// alert('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!suggest OK');
        delete data.status;
        // printObject(data);
        dataArr = new Array()
        // for (var p in data)
            // dataArr.push($.parseJSON(data[p]));
			
		for (var i in data) {			// JSON comatibility on server side never implemented
			dataArr[i] = data[i];
		}
		
        GData.suggestionList = dataArr;
       
		alert('Retrived ' + dataArr.length + ' suggestions from server');
		
		var sc8 = $.sfScene.get('Scene8');
		if(sc8) {
			sc8.updateList(GData);
		}
		
    } else {
		// alert('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!suggest not OK');
	}
}


        
function GCProfileLoaded(data)
{
    if (GCcheckData(data))
    {
        // printObject(data);
        alert('add profile data parsing');
        delete data.status;
        GData.profiles[data.name] = data;
        if (GData.currentProfile == data.name)
        {
            transferDataToControls();
        }
    }
}
function GCProfileSent(data)
{
    if (GCcheckData(data))
        alert('Profile succesfully uploaded');
}
function Server() 
{
    this.logger = new Logger('SERVER');
    this._ServerAddres = 'http://simple-interactive-tv.appspot.com'
    this.requestType = 'get';  
    this.widgetId = 'widget1';
}

Server.prototype.sendProfile = function(profile)
{
    this._makeRequest('usersServlet',{ action : 'setProfile' , profile: $.toJSON(profile)},GCProfileSent);
}

Server.prototype.getSuggestions = function(profile)
{
	// alert('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!suggesting');
	// printObject(profile);
	// alert($.toJSON(profile));
	// this._makeRequest('suggestions',{ 'profile' : $.toJSON(profile) },GCSuggestions);		// this was planned change for server - never completed
	this._makeRequest('suggestions',{'brutality':profile.MovieBrutality,'romantism':profile.MovieRomance,'comedy':profile.MovieHumor,'startTime':123456,'endTime':234567} ,GCSuggestions);
};

Server.prototype.getProfiles = function(widgetId)
{
    this._makeRequest('usersServlet',{ 'widgetId' : this.widgetId, 'action' : 'getProfiles'},GCProfilesListed);
};
Server.prototype.removeProfile = function(profileId)
{
    this._makeRequest('usersServlet',{'widgetId': this.widgetId , 'action' : 'deleteProfile' , 'name' : profileId },null);
}

Server.prototype.getProfile = function(profileId)
{
    this._makeRequest('usersServlet',{ 'widgetId' : 'widget1', 'action' : 'getProfile', 'name' : profileId},GCProfileLoaded);
};
Server.prototype._makeRequest = function(servlet,params,callback)
{
	// alert('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
	// printObject(params);
    this.logger.log('makeRequest','Making request to servlet ' + servlet);
    jQuery.getJSON(this._ServerAddres + '/' + servlet,params,callback);
    this.logger.log('makeRequest','Request send');
}

var ServerProxy = new Server();

alert('Server.js loaded');
CodeRepo.SetReady('Server');